import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

public class VectorMario {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		In in = new In(args[0]);
		int rows = in.readInt();
		int cols = in.readInt();
		char[][] map = new char[rows][cols];
		ArrayList<int[]> start = new ArrayList<int[]>();
		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < cols; j++) {
				char c = in.readChar();
				if (c == '\n')
					c = in.readChar();
				assert c != '\n';
				if (c == 'S')
					start.add(new int[] { i, j });
				map[i][j] = c;
			}
		}
		assert in.isEmpty();
		in.close();

		// SEARCH
		// int velX = 0;
		// int velY = 0;
		HashMap<FieldMove, FieldMove> visited = new HashMap<FieldMove, FieldMove>();
		ArrayDeque<FieldMove> queue = new ArrayDeque<FieldMove>();
		for (int[] s : start) {
			FieldMove f = new FieldMove(s[0], s[1], 0, 0);
			queue.offer(f);
			visited.put(f, null);
		}
		search:
		while (!queue.isEmpty()) {
			FieldMove current = queue.poll();
			int x = current.x + current.vX;
			int y = current.y + current.vY;
			for (int i = -1; i <= 1; i++) {
				for (int j = -1; j <= 1; j++) {
					if (x + i < 0 || x + i >= rows ||
						y + j < 0 || y + j >= cols ||
						map[x+i][y+j] == 'O') ; // DO NOTHING
					else if (map[x+i][y+j] == 'F') {
						int steps = 1;//en for start- og en for slut steppet
						StdOut.println("x:" + (x+i) + " y:" + (y+j) + " vX:" + (current.vX + i) + " vY:" + (current.vY + j));
						while (visited.containsKey(current)) {
							System.out.println(current);
							current = visited.get(current);
							steps++;
						}
						StdOut.println(current);
						StdOut.println(steps);
						break search;
					} else {
						FieldMove fm = new FieldMove(x + i, y + j, current.vX
								+ i, current.vY + j);
						
						if (visited.containsKey(fm)) {
							// nothing it should have the right steps do to BFS
						} else {
							visited.put(fm, current);
							queue.offer(fm);
						}
					}
				}
			}
		}
		in = new In(args[0].substring(0, args[0].indexOf('.')) + ".out");
		StdOut.println(in.readAll());
		in.close();
	}
}

class FieldMove {
	public final int x, y, vX, vY;

	public FieldMove(int x, int y, int vX, int vY) {
		this.x = x;
		this.y = y;
		this.vX = vX;
		this.vY = vY;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + vX;
		result = prime * result + vY;
		result = prime * result + x;
		result = prime * result + y;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		FieldMove other = (FieldMove) obj;
		if (vX != other.vX)
			return false;
		if (vY != other.vY)
			return false;
		if (x != other.x)
			return false;
		if (y != other.y)
			return false;
		return true;
	}
	
	public String toString() {
		return "x:" + x + " y:" + y + " vX:" + vX + " vY:" + vY;
	}
}